package dsf.eshop.user.web;

import dsf.eshop.commons.base.BaseResult;
import dsf.eshop.user.dao.UserMapper;
import dsf.eshop.user.entity.User;
import dsf.eshop.user.entity.UserExample;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class UserController {

    @Resource
    UserMapper userMapper;

    @ApiOperation("获取用户列表")
    @GetMapping("user/list")
//    @RequiresPermissions("user:list")
    @RequiresRoles("admin")
    public String userList(Integer limit, Integer offset, Map<String, Object> map) {

        if(limit == null) {
            limit = 20;
        }
        if(offset == null) {
            offset = 0;
        }
        List<User> users = userMapper.selectByExampleAndPage(null, limit, offset);
        log.info("user list {}", users);
        map.put("users", users);
        return "user/list";
    }

    @ApiOperation("获取用户详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", dataType = "int")})
    @RequiresUser
    @GetMapping("user/{userId}")
    public String detail(@PathVariable("userId")int userId, Map<String, Object> map) {

        User user = userMapper.selectByPrimaryKey(userId);
        map.put("user", user);
        return "user/detail";
    }


    @PutMapping("user/{userId}")
    @RequiresUser
    public BaseResult modify(@PathVariable("userId")int userId, User user) {

        if(userMapper.selectByPrimaryKey(userId) == null) {
            return new BaseResult(1, "用户不存在");
        } else if(user.getUserId() != userId) {
            return new BaseResult(1, "请重试");
        }
        int i = userMapper.updateByPrimaryKeySelective(user);
        if(1 == i) {
            return new BaseResult(0, "success");
        } else
            return new BaseResult(1, "修改失败");
    }

    @PostMapping("")
    public BaseResult add(User user) {

        int i = userMapper.insertSelective(user);
        if(1 == i) {
            return new BaseResult(0, "添加成功");
        } else {
            return new BaseResult(1, "添加失败 请稍后重试");
        }
    }

    @RequiresPermissions("user:delete")
    @DeleteMapping("user/{userId}")
    public BaseResult deletUser(@PathVariable("userId")int userId) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUserIdEqualTo(userId);
        if(1 != userMapper.countByExample(userExample))
            return new BaseResult(1, "用户不存在");
        User user = new User();
        user.setUserId(userId);
        user.setStatus(1);
        int i = userMapper.updateByPrimaryKeySelective(user);
        log.info("删除用户》》》"+ i);
        return new BaseResult(0, "删除成功");
    }

    @DeleteMapping("")
    public BaseResult deleteUsers(List<Integer> users) {

        ArrayList<Integer> unable = new ArrayList<>();
        for(int user : users) {
//            userMapper
            unable.add(user);
        }
        return null;

    }
}
